package com.google.android.apps.mytracks.casos;

import com.google.android.apps.mytracks.content.Caso;
import com.google.android.apps.mytracks.content.CasosColumns;
import com.google.android.apps.mytracks.content.MyTracksProvider.DatabaseHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class CasoDataSource {

  // Database fields
  private SQLiteDatabase database;
  private DatabaseHelper dbHelper;
  private String[] allColumns = CasosColumns.COLUMNS;

  public CasoDataSource(Context context) {
    dbHelper = new DatabaseHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public long createCaso(Caso caso) {
    ContentValues values = new ContentValues();
    values.put(CasosColumns.NOMBRE, caso.getNombre());
    values.put(CasosColumns.CLIENTEID, caso.getClienteId());
    values.put(CasosColumns.FECHAINICIO, caso.getFechaInicio());
    values.put(CasosColumns.FECHAFIN, caso.getFechaFin());
    long insertId = database.insert(CasosColumns.TABLE_NAME, null, values);
    return insertId;
  }
  
  public Caso getCaso(long casoId) {
    String[] args = new String[] {String.valueOf(casoId)};
    Cursor cursor = database.query(CasosColumns.TABLE_NAME,
        allColumns, CasosColumns._ID+"=?", args, null, null, null);
    cursor.moveToFirst();
    Caso caso = cursorToCaso(cursor);
    cursor.close();
    return caso;
  }
  

  
  public void deleteCaso(Caso caso) {
    long id = caso.getId();
    database.delete(CasosColumns.TABLE_NAME, CasosColumns._ID
        + " = " + id, null);
  }
  
  public void updateCaso(Caso caso) {
    
    long id = caso.getId();
    ContentValues values = new ContentValues();
    values.put(CasosColumns.NOMBRE, caso.getNombre());
    values.put(CasosColumns.CLIENTEID, caso.getClienteId());
    values.put(CasosColumns.FECHAINICIO, caso.getFechaInicio());
    values.put(CasosColumns.FECHAFIN, caso.getFechaFin());
    String strFilter = "_id=" + id;
    database.update(CasosColumns.TABLE_NAME, values, strFilter, null);
  }

  public List<Caso> getAllcasos() {
    List<Caso> casos = new ArrayList<Caso>();

    Cursor cursor = database.query(CasosColumns.TABLE_NAME,
        allColumns, null, null, null, null, CasosColumns.DEFAULT_SORT_ORDER + "DESC");

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Caso caso = cursorToCaso(cursor);
      casos.add(caso);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return casos;
  }

  private Caso cursorToCaso(Cursor cursor) {
    Caso caso = new Caso();
    caso.setClienteId(cursor.getLong(1));
    caso.setAsunto(cursor.getString(2));
    caso.setFechaInicio(cursor.getString(3));
    caso.setFechaFin(cursor.getString(4));
    caso.setNombre(cursor.getString(5));
    return caso;
  }
} 